package cn.edu.cidp.www.myapplication

import android.content.Context
import android.util.Log
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.spring
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.navigation.NavController
import cn.hutool.core.io.FileUtil
import coil.compose.rememberImagePainter
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import java.io.File
import java.util.Collections

@Composable
fun previewCompose(
    navController: NavController,
    lifecycleScope: LifecycleCoroutineScope){
    Log.d("PagerContent1", "previewCompose: 检测最外层渲染时机")
    PagerContent1(
        onUploadClick = {
            if (FileUtil.size(File(it)) <= 2097152){
                FileUtilsImpl().update(it,lifecycleScope)   // 单独上传
            }else{
                FileUtilsImpl().chunkUpdate(fileName = it, lifecycleScope)  //分片上传
            }
        }
    )
}
//引导页案例1
@OptIn(ExperimentalPagerApi::class)
@Composable
fun PagerContent1(
    onUploadClick: (String) -> Unit
) {
    val context = LocalContext.current
    val path = FileUtilsImpl().getPhotoPath(context)
    var ergodicFiles2 = remember {
        mutableStateOf<List<String>>(FileUtilsImpl().ergodicFiles(path))
//        FileUtilsImpl().ergodicFiles(path).toMutableStateList()
    }
    val state = rememberPagerState(initialPage = 0)

    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Log.d("PagerContent1", "ergodicFiles2.size: ${ergodicFiles2.value.size}")
        HorizontalPager(count = ergodicFiles2.value.size, state = state) { pager ->
            val fileName = path + File.separator + ergodicFiles2.value[pager]
            pagerContent(context, fileName, onDeleteClick = {
                FileUtilsImpl().delete(fileName)
                ergodicFiles2.value = FileUtilsImpl().ergodicFiles(path)
                Log.d("PagerContent1", "PagerContent1: ${ergodicFiles2.value.size}")
                Log.d("PagerContent1", "pager: $pager")
            },
                onUploadClick = { onUploadClick(fileName) }
            )
        }
//        Indicator(size = items.size, index = state.currentPage)

/*        val width = animateDpAsState(
            targetValue = if (state.currentPage.inc() == items.size) 60.dp else 0.dp,
            animationSpec = if (state.currentPage.inc() == items.size)
                spring(dampingRatio = Spring.DampingRatioHighBouncy)
            else
                spring(dampingRatio = Spring.DampingRatioMediumBouncy)
        )
        Row(
            modifier = Modifier.height(100.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            FloatingActionButton(
                onClick = {

                },
                Modifier
                    .size(width.value)
            ) {
                Icon(
                    imageVector = Icons.Default.ArrowForward,
                    contentDescription = "",
                    tint = Color.Black
                )
            }
        }*/
    }
}

@Composable
private fun pagerContent(
    context : Context,
    fileName : String,
    onDeleteClick: () -> Unit,
    onUploadClick: () -> Unit
) {
    Box(modifier = Modifier.fillMaxSize()){
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.White),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Image(
                painter = rememberImagePainter(data = FileUtilsImpl().openFile(context, fileName)),
                contentScale = ContentScale.Fit,
                contentDescription = "",
//                modifier = Modifier.rotate()
            )
        }
        Text(text = fileName, modifier = Modifier.padding(top = 10.dp))
        Row(modifier = Modifier
            .fillMaxWidth()
            .padding(20.dp)
            .align(Alignment.BottomCenter),
            horizontalArrangement = Arrangement.SpaceAround,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Button(onClick = onDeleteClick
            ) {
                Text(text = "删除")
            }
            Button(onClick = onUploadClick) {
                Text(text = "上传")
            }
        }
    }
    
}

@Composable
private fun IndicatorHorizontal(isSelected: Boolean) {
    val width = animateDpAsState(
        targetValue = if (isSelected) 30.dp else 10.dp,
        animationSpec = spring(dampingRatio = Spring.DampingRatioHighBouncy)
    )
    Box(
        modifier = Modifier
            .height(10.dp)
            .width(width.value)
            .clip(CircleShape)
            .background(if (isSelected) Color.Black else Color.LightGray)
    )
}

@Composable
private fun Indicator(size: Int, index: Int) {
    Column() {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(31.dp)
        ) {
            repeat(size) {
                IndicatorHorizontal(it == index)
            }
        }
        Text(text = "拍照  录像")
    }
}

private data class ItemData(
    val image: Int,
    val title: String,
    val content: String
)

